<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            transition: 2s;
            padding: 0;
            margin: 0;
            font-family: system-ui, -apple-system, BlinkMacSystemFont,
                'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
        }

        .box {
            width: 300px;
            margin: 50px auto;
            font-size: 18px;
        }

        .form-group label {
            display: block;
            margin-bottom: 10px;
            margin-top: 5px;
        }

        .form-group input {
            width: 100%;
            height: 25px;

        }

        #content {
            width: 100%;
        }

        button {
            padding: 5px 10px;
            margin-top: 10px;
            margin-bottom: 20px;
        }

        span {
            color: red;
            font-size: 12px;
            font-weight: bold;
        }

        .content {
            width: 100%;
            background-color: blueviolet;
            color: white;
            position: relative;
        }

        li {
            list-style: none;
            padding: 20px;
        }

        h4 {
            display: inline;

        }

        small {
            float: right;
        }

        p {
            font-size: 15px;
            margin-top: 10px;
        }

        a {
            text-decoration: none;
            float: right;
            color: plum;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="form-group">
            <label for="username">姓名:</label>
            <input type="text" id="username">
            <span></span>
        </div>
        <div class="form-group">
            <label for="content">留言内容:</label>
            <textarea id="text" rows="5"></textarea>
            <span></span>
        </div>
        <button>提交</button>


        <div class="content">
            <ul></ul>
            
</body>
</html>
<script>
    var btn = document.querySelector('button');
    var username = document.getElementById('username');
    var text = document.querySelector('#text')
    var content = document.querySelector('#content');
    var span = document.querySelectorAll('span');
    var content = document.querySelector('.content');
    var ul = document.querySelector('ul');

    btn.addEventListener('click', function () {
        if (username.value == "") {
            span[0].innerHTML = '请输入用户名'

        }
        else if (text.value == "") {
            span[1].innerHTML = '请输入留言内容'
        }
        else {
            span[0].innerHTML = ''
            span[1].innerHTML = ''

            var li = document.createElement('li');
            var h4 = document.createElement('h4');
            li.appendChild(h4);
            h4.innerHTML = username.value;

            var small = document.createElement('small');
            li.appendChild(small);
            small.innerHTML = getdate();

            var p = document.createElement('p');
            li.appendChild(p);

            p.innerHTML = text.value + '<a href="javascript:;" >删除</a>';
            content.insertBefore(li, content.children[0]);
            // content.appendChild(li);


            var as = document.querySelectorAll('a');

            for(var i=0;i<as.length;i++){
                as[i].onclick=function(){
                    if(confirm("确定要删除吗？")){
                        content.removeChild(this.parentNode.parentNode);
                    }else{

                    }
                }
            }
            }
        })
            function getdate() {
                var date = new Date();
                var str = date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日';
                str += '&nbsp'+'&nbsp' + date.getHours() + ':' + date.getMinutes() + ':' + date.getMinutes();
                return str;
            }
            console.log(getdate());
</script>
